<template>
  <div class="app-container">
    <el-row :gutter="10">
    
      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
          <el-form-item label="商品名称：" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable />
          </el-form-item>
          <el-form-item label="订单状态：" prop="categoryId">
            <el-select v-model="queryParams.status" filterable clearable placeholder="请选择状态">
              <el-option v-for="(item) in moreArr" :key="item.value" :label="item.label" :value="item.value.toString()">
              </el-option>
            </el-select>
          </el-form-item>
        
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>

    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="订单编号" align="center" prop="id" width="180" />
      <el-table-column label="图片" align="left" prop="imgUrl" >
        <template #default="scope">
          <div class="img-box">
            <el-image style="width: 100px; height: 50px" :src="scope.row.imgUrl" fit="cover" :preview-teleported="true">
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" align="center" prop="productId" width="80" />
      <el-table-column label="商品名称  " align="center" prop="productName" width="180" />
      <el-table-column label="供应商" align="center" prop="providerName" width="180" />
      <el-table-column label="商品数量" align="center" prop="num" width="80" />
      <el-table-column label="单价" align="center" prop="price" width="80" />

      <el-table-column label="总金额" align="center" prop="totalPay" width="180" />
      <el-table-column label="订单状态" align="center" prop="statusName" width="180" />
      <el-table-column label="订单地址" align="center" prop="address" width="180" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link size="small" type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.size"
      @pagination="getImgApi" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog title="编辑订单信息" v-model="open" width="600px" append-to-body>
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="120px">
        <!-- <el-form-item label="订单编号:" prop="name" >
          <el-input v-model="editForm.name" disabled />
        </el-form-item> -->
        <el-form-item label="商品单价:" prop="price">
          <el-input v-model="editForm.price" ></el-input>
        </el-form-item>
        <el-form-item label="商品数量:" prop="num">
          <el-input v-model="editForm.num"></el-input>
        </el-form-item>
        <el-form-item label="订单地址:" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="订单状态:" prop="status">
          <el-select v-model="editForm.status" filterable clearable placeholder="请选择订单状态">
            <el-option v-for="(item) in moreArr" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer" v-loading="loading">
          <el-button @click="open = false">取 消</el-button>
          <el-button type="primary" @click="submitForm()">保 存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import UpFile from "@/components/upFile/index.vue";
import { addFile, upFileReq, getImg, delFile } from "@/api/components/index.js";
import {delProduct, getProductOrderList,delProductOrder,editProductOrder} from "@/api/product/index.js"
import { reactive } from "vue";

const { proxy } = getCurrentInstance();
const list = ref([]);
const open = ref(false);
const loading = ref(false);
const fileList = ref([]);
const upFileRef = ref(null);
const dialogTitleText = ref('')
const total = ref(0);
// const orderStatus = reactive([
//   {value:1,''}
// ])


let moreArr = reactive([
  { value: 1, label: '已接收' },
  { value: 2, label: '已审核' },
  { value: 3, label: '已发货' },
  { value: 4, label: '已退货' },

]);

let fileId;
const data = reactive({
  form: {},
  editForm: {
    num: '',
    address: '',
    price: '',
    status: '',
    id:'',
  },
  queryParams: {
    page: 1,
    size: 10,
    name:'',
    status:''
  },
});
const { editForm, form, queryParams } = toRefs(data);
function handleAdd() {
  open.value = true;
  dialogTitleText.value = '新增商品'
  fileId = undefined;
  nextTick(() => {
    upFileRef.value.clearFiles();
  })
}
async function handleUpdate(row) {
  open.value = true;
  fileId = row.id;
  nextTick(() => {
    fileList.value = imgEvent(row);
  })
  editForm.value.price = row.price
  editForm.value.num  = row.num
  editForm.value.address = row.address
  editForm.value.status = Number(row.status)
  editForm.value.id = row.id
}
async function submitForm() {

    try {
      loading.value = true;
      const data = { ...editForm.value};
      !form.value.id && await editProductOrder({ ...data });
      // form.value.id&&await upUser(data);
      proxy.$modal.msgSuccess( "修改成功");
      open.value = false;

      getProductLists();
    } finally {
      loading.value = false;
    }

  

}
async function handleDelete(row) {
  await proxy.$modal.confirm("确认删除当前订单吗？");
  await delProductOrder({ id: row.id });
  proxy.$modal.msgSuccess("删除成功");
  getProductLists();

}
async function getProductLists() {
  try {
    loading.value = true;
    const { data, total: totalAll } = await getProductOrderList(queryParams.value);
    total.value = totalAll;
    list.value = data;
    list.value.forEach(ele => {
      if(ele.status == '1' ){
        ele.statusName = '已接收'
      }
      if(ele.status == '2' ){
        ele.statusName = '已审核'
      }
      if(ele.status == '3' ){
        ele.statusName = '已发货'
      }
      if(ele.status == '4' ){
        ele.statusName = '已退货'
      }
      ele.price = ele.price
      ele.vipPrice = ele.vipPrice
      ele.cuxiaoPrice = ele.cuxiaoPrice

    })
  } finally {
    loading.value = false;
  }
}
function handleQuery() {
  queryParams.value.page = 1;
  getProductLists();
}
function imgEvent(row) {
  try {
    return JSON.parse(row.val)
  } catch (e) {
    return []
  }
}
// getImgApi();
getProductLists()
</script>

<style scoped lang="scss">
.flex-box {
  text-align: center;
}

.img-box {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  overflow: auto;

  >div {
    margin: 0 5px;
  }
}
</style>